package com.tool;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




import cn.edu.hfut.dmic.webcollector.util.JDBCHelper;

import com.model.xmlElement.DataColumn;
import com.model.xmlElement.DataColumns;
import com.model.xmlElement.output.DataBase;

public class DaoTool {
	public static final Logger LOG = LoggerFactory.getLogger(DaoTool.class);
	
	public static void createTable(String templateName,DataBase dataBase,DataColumns dataColumns){
		StringBuilder sql = new StringBuilder(200);
		sql.append("CREATE TABLE IF NOT EXISTS ").append(dataBase.getDataBaseTableName())
		.append(" (")
		.append("id int(11) NOT NULL AUTO_INCREMENT,");
		for(DataColumn dataColumn : dataColumns){
			sql.append(dataColumn.getDataColumnOutput().getOutputColumn()).append(" longtext,");
		}
		sql.append(" PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
		execute(templateName,sql.toString());
	}
	
	public static void insert(String templateName,DataBase dataBase,String keys,String values,String line){
		StringBuilder sql = new StringBuilder(200);
		sql.append("insert ignore into ").append(dataBase.getDataBaseTableName())
		.append(" ( ").append(keys).append(" ) ")
		.append(" values ( ").append(values).append(" ); ");
		String[] data = line.split(",");
		LOG.info(sql.toString());
		JDBCHelper.getJdbcTemplate(templateName).update(sql.toString(),data);
	}
	
	/**
	 * 执行
	 * @param sql
	 */
	public static void execute(String templateName,String sql){
		LOG.info("sql->" + sql);
		JDBCHelper.getJdbcTemplate(templateName).execute(sql);
	}
	
	public static void update(String templateName,String sql,Object ... params){
		LOG.info("sql->" + sql);
		JDBCHelper.getJdbcTemplate(templateName).update(sql,params);
	}
	
}
